/** 
  Copyright (c) 2008, 2009 Claudio Imbrenda
  This program is released under the terms of the
  GNU GPL version 3 or later.
*/
package macchina.nastri;

import java.io.*;
import macchina.*;

public class NumberFileNastro implements AdattatoreEntrata{

  BufferedReader inf;
  boolean isloop;
  
  public NumberFileNastro(File f)throws FileNotFoundException{
    inf=new BufferedReader(new FileReader(f));
  }
  public NumberFileNastro(String s) throws FileNotFoundException{
    inf=new BufferedReader(new FileReader(s));
  }
  
  
  private boolean ioerr=false;
  
  public byte read(){
    byte rr=AdattatoreEntrata.EOT|AdattatoreEntrata.IOERR;
    if(ioerr)
      return AdattatoreEntrata.EOT|AdattatoreEntrata.IOERR;
    
    try{
      rr = Byte.parseByte(inf.readLine());
    }
    catch(IOException ignore){ioerr=true;return read();}
    catch(NumberFormatException ignore){ioerr=true;return read();}
    catch(NullPointerException ignore){
        try{
            if(isloop){
                inf.reset();
                return read();
            }
        }catch(IOException e){ioerr=true;return read();}
        rr=-1;
    }
    
    if(rr==-1)return 32;
    return MRUtils.toMR((char)rr);
  }

    public void setLoop(boolean v) {
        isloop=v;
    }

    public boolean isLoop() {
        return isloop;
    }

}
